// @file     YCOAServletContainerTool.groovy
// @author   Mac Radigan
// @version  $Id: YCOAServletContainerTool.groovy 30 2011-04-10 04:12:24Z mac.radigan $

package ycoa.servlet

import sys.tools.Tool
import sys.utilities.ResourceManager
import ycoa.servlet.YCOAServletContainer
import ycoa.configuration.YCOAConfiguration

import org.mortbay.jetty.*
import org.mortbay.jetty.nio.*
import org.mortbay.jetty.servlet.*
import org.mortbay.jetty.deployer.*
import org.mortbay.jetty.handler.*
import org.mortbay.jetty.webapp.*
import groovy.servlet.*

import org.apache.log4j.Logger

public class YCOAServletContainerTool extends Tool {

  static Logger log = Logger.getLogger(YCOAServletContainerTool.class)

  public String getName() {
    return "ycoa-servlet-container"
  }

  public String getDescription() {
    return "YCOA servlet container."
  }

  public String run(String[] args) {
    def config = YCOAConfiguration.getConfiguration()
    log.debug("starting YCOAServletContainerTool")
    def cli = new CliBuilder(usage: "${getName()} <options> [-h]")  
    cli.d(longOpt:'directory', 'directory', required:false, args:1)
    cli.p(longOpt:'port', 'port', required:false, args:1)
    cli.h(longOpt:'help', 'usage information', required:false)
    OptionAccessor opt = cli.parse(args); if(!opt) { return }
    def port = 80
    try {
      def resourceManager = new ResourceManager()
      resourceManager.extractResource("webapps", new File("webapps"))
      resourceManager.extractResource("modules", new File("modules"))
      if(opt.p) port = Integer.parseInt(opt.p)
      log.error("server starting on port ${port}")
      def ycoaServletContainer = new YCOAServletContainer()
      if(opt.d) {
        ycoaServletContainer.start(port, opt.d)
      } else {
        ycoaServletContainer.start(port)
      }
    } catch(e) {
      log.error("${e.getMessage()}")
    }
  }

}

// *EOF*
